package com.example.administrator.fastdream.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.bean.LookAllRecordBean;
import com.example.administrator.fastdream.date.presenter.LookAllRecordPresenter;
import com.example.administrator.fastdream.view.base.BaseActivity;
import com.example.administrator.fastdream.view.base.BaseDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LookAllRecordActivity extends BaseActivity implements LookAllRecordPresenter.LookAllRecordCallBack {

    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private BaseDialog mDialog;
    private BaseDialog.Builder mBuilder;
    private LookAllRecordPresenter presenter;
    private int page = 1;
    private RecordAdapter recordAdapter;
    private TextView tvCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_all_record);
        ButterKnife.bind(this);
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("圆梦故事");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        presenter = new LookAllRecordPresenter(this, this);
        presenter.lookAllRecord(page);
        initListener();
    }

    private void initListener() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                presenter.lookAllRecord(page);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                presenter.lookAllRecord(page);
            }
        });
    }

    @Override
    public void lookAllRecord(LookAllRecordBean lookAllRecordBean) {
        final List<LookAllRecordBean.DataEntity.DataEntity2> data = lookAllRecordBean.getData().getData();
        if (recordAdapter == null) {
            recordAdapter = new RecordAdapter(R.layout.item_all_record, data);
            View view = View.inflate(mContext, R.layout.recycler_header, null);
            ImageView iv_bg = view.findViewById(R.id.iv_bg);
            iv_bg.setBackgroundResource(R.drawable.yuanmenggushi_bg);
            tvCount = view.findViewById(R.id.tv_count);
            recordAdapter.addHeaderView(view);
            tvCount.setText("累计帮助" + lookAllRecordBean.getData().getDream_number() + "人圆梦，" + lookAllRecordBean.getData().getLucky_number() + "人获得幸运红包");
            recyclerView.setAdapter(recordAdapter);
        } else {
            tvCount.setText("累计帮助" + lookAllRecordBean.getData().getDream_number() + "人圆梦，" + lookAllRecordBean.getData().getLucky_number() + "人获得幸运红包");
            if (page == 1) {
                recordAdapter.setNewData(data);
                refreshLayout.finishRefresh();
            } else {
                if (data == null || data.size() == 0) {
                    Toast.makeText(this, "到底啦~", Toast.LENGTH_SHORT).show();
                    refreshLayout.finishLoadMore();
                    return;
                }
                recordAdapter.addData(data);
                refreshLayout.finishLoadMore();
            }
        }
        recordAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_heshi) {
                    Intent intent = new Intent(mContext, DreamDetailActivity.class);
                    intent.putExtra("uid", data.get(position).getUid());
                    startActivity(intent);
                }
                if (view.getId() == R.id.tv_number) {
                    Intent intent = new Intent(mContext, JiangDetailActivity.class);
                    intent.putExtra("issue", data.get(position).getIssue());
                    startActivity(intent);
                }
            }
        });
    }

    private class RecordAdapter extends BaseQuickAdapter<LookAllRecordBean.DataEntity.DataEntity2, BaseViewHolder> {

        public RecordAdapter(int layoutResId, @Nullable List<LookAllRecordBean.DataEntity.DataEntity2> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, LookAllRecordBean.DataEntity.DataEntity2 item) {
            helper.addOnClickListener(R.id.tv_heshi).addOnClickListener(R.id.tv_number);
            Glide.with(mContext).load(item.getAvatar()).into((ImageView) helper.getView(R.id.iv_head));
            String issue = item.getIssue();
            String time = issue.substring(0, 4) + "年" + issue.substring(4, 6) + "月" + issue.substring(6, 8)
                    + "日第" + issue.substring(8, issue.length()) + "期";
            helper.setText(R.id.tv_name, item.getUsername())
                    .setText(R.id.tv_dream, item.getDream_name())
                    .setText(R.id.tv_money, item.getDream_money() + "元")
                    .setText(R.id.tv_luck_number, item.getLucky_number())
                    .setText(R.id.tv_number, item.getOpen_number())
                    .setText(R.id.tv_time, time)
                    .addOnClickListener(R.id.tv_dream);
        }
    }

    private void showHeshiDialog() {
        mBuilder = new BaseDialog.Builder(this);
        mDialog = mBuilder.setViewId(R.layout.dialog_record_heshi)
                //设置dialogpadding
                .setPaddingdp(20, 0, 20, 0)
                //设置显示位置
                .setGravity(Gravity.CENTER)
                //设置动画
                .setAnimation(R.style.Alpah_aniamtion)
                //设置dialog的宽高
                .setWidthHeightpx(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                //设置触摸dialog外围是否关闭
                .isOnTouchCanceled(true)
                //设置监听事件
                .builder();
        mDialog.show();
        mDialog.getView(R.id.tv_yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mDialog.dismiss();
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
